package com.jkwzms.base.xmb.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jkwzms.base.xmb.entity.XmbModel;
import com.jkwzms.base.xmb.service.XmbService;
import com.jkwzms.base.xmb.util.ExcelUtil;
import com.jkwzms.common.Constants;
import com.jkwzms.common.entity.search.SearchRequest;
import com.jkwzms.common.entity.search.Searchable;
import com.jkwzms.common.inject.annotation.BaseComponent;
import com.jkwzms.common.pagination.Pagination;
import com.jkwzms.common.web.Servlets;
import com.jkwzms.common.web.controller.BaseCRUDController;


@Controller
@RequestMapping(value ="/base/xmb")
public class XmbController extends BaseCRUDController<XmbModel, Long>{
    
    @Autowired
    @BaseComponent
    private XmbService  xmbService;
    
    @RequestMapping(value = "view")
    public String view(){
        return "base/xmb/list";
    }
    
    /**
     * 获取页面数据
     */
    @RequestMapping(value = "list")
    @ResponseBody
    public Pagination<XmbModel> list(@RequestParam("pageNumber") int pageNumber,@RequestParam("pageSize") int pageSize, HttpServletRequest request){
        Pagination<XmbModel> list = null;
        Map<String, Object> searchParams = Servlets.getParametersStartingWith(request, Constants.SEARCH_PREFIX);
        Searchable searchable = SearchRequest.newSearchable();
        searchable.addSearchParams(searchParams);
        searchable.setPage(pageNumber - 1, pageSize);
        list = xmbService.findAll(searchable);
        return list;
    }
    
    /**
     * 添加部门信息
     * @param xmb
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "addMessage")
    public Map<String, Object> add(XmbModel xmb,HttpServletRequest request ,HttpServletResponse response){
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            xmbService.save(xmb);
            map.put("result", true);
            map.put("msg", "添加成功！");
            return map;
        } catch (Exception e) {
            // TODO: handle exception
            map.put("result", false);
            map.put("message", "添加失败！");
            return map;
        }
        
    }
    
    /**
     * 修改部门信息
     * @param xmb
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "editMessage")
    public Map<String, Object> edit(XmbModel xmb,HttpServletRequest request ,HttpServletResponse response){
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            xmbService.update(xmb);
            map.put("result", true);
            map.put("msg", "修改成功！");
            return map;
        } catch (Exception e) {
            // TODO: handle exception
            map.put("result", false);
            map.put("message", "修改失败！");
            return map;
        }
        
    }
    
    /**
     * 批量删除部门信息
     * @param ids
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "deleteByIds")
    public Map<String, Object> delete(@RequestParam (value = "ids")String ids,HttpServletRequest request ,HttpServletResponse response){
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            String[] id = ids.split(",");
            for(int i =0;i<id.length; i++){
                xmbService.delete(Long.parseLong(id[i]));
            }
            map.put("result", true);
            map.put("msg", "删除成功！");
            return map;
        } catch (Exception e) {
            // TODO: handle exception
            map.put("result", false);
            map.put("message", "删除失败！");
            return map;
        }
    }
    
    /*
     * 获取全部列表信息
     */
    public List<XmbModel> getAll(){
        return xmbService.findAll();
    }
    
    /**
     * 导出
     * @param resp
     * @param request
     */
    @RequestMapping(value ="exceloutput")
    public void ExcleOutPut(HttpServletResponse resp, HttpServletRequest request) {
        Pagination<XmbModel> list = null;
        Map<String, Object> searchParams = Servlets.getParametersStartingWith(request, Constants.SEARCH_PREFIX);
        Searchable searchable = SearchRequest.newSearchable();
        searchable.addSearchParams(searchParams);
        searchable.setPage(0, 10000);
        list = xmbService.findAll(searchable);
        HSSFWorkbook  workbook = (HSSFWorkbook) ExcelExportUtil.exportExcel(new ExportParams("项目部信息", "", ""), XmbModel.class, list.getRows());
        String filePath = "项目部信息导出.xls";
        try {
            ExcelUtil.getInstance().export(workbook, filePath, resp, request);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
